package com.project.server.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.project.server.entity.Article;
import com.project.server.mapper.ArticleMapper;
import com.project.server.service.ArticleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class ArticleServiceImpl extends ServiceImpl<ArticleMapper,Article> implements ArticleService {

    @Autowired
    private ArticleMapper articleMapper;

    @Override
    public Page<Article> getArticlePage(Integer current, Integer size, String title,Long categoryId,Integer status) {
        Page<Article> page = new Page<>(current, size);
        LambdaQueryWrapper<Article> wrapper = new LambdaQueryWrapper<Article>()
            .eq(categoryId != null, Article::getCategoryId, categoryId)
            .like(title != null && !"".equals(title) , Article::getTitle, title)
            .eq(status != null, Article::getStatus, status)
            .orderByDesc(Article::getCreateTime);
        return articleMapper.selectPage(page, wrapper);
    }

    @Override
    public Article getArticleDetail(Long id) {
        return articleMapper.selectById(id);
    }

    @Override
    @Transactional
    public void incrementViewCount(Long id) {
        Article article = articleMapper.selectById(id);
        if (article != null) {
            article.setViewCount(article.getViewCount() + 1);
            articleMapper.updateById(article);
        }
    }

    @Override
    public List<Article> getRecommendArticles(int limit) {
        return articleMapper.selectList(
            new LambdaQueryWrapper<Article>()
                .eq(Article::getStatus, 1)
                .orderByDesc(Article::getViewCount)
                .last("LIMIT " + limit)
        );
    }

    @Override
    @Transactional
    public void saveArticle(Article article) {
        article.setCreateTime(LocalDateTime.now());
        article.setUpdateTime(LocalDateTime.now());
        article.setViewCount(0);
        articleMapper.insert(article);
    }

    @Override
    @Transactional
    public void updateArticle(Article article) {
        article.setUpdateTime(LocalDateTime.now());
        articleMapper.updateById(article);
    }

    @Override
    @Transactional
    public void deleteArticle(Long id) {
        articleMapper.deleteById(id);
    }

    @Override
    public Map<String, Object> getArticleStats() {
        Map<String, Object> stats = new HashMap<>();

        // 总文章数
        stats.put("totalArticles", articleMapper.selectCount(null));

        // 已发布文章数
        stats.put("publishedArticles", articleMapper.selectCount(
            new LambdaQueryWrapper<Article>()
                .eq(Article::getStatus, 1)
        ));

        // 总浏览量
        Long totalViews = articleMapper.selectList(null)
            .stream()
            .mapToLong(Article::getViewCount)
            .sum();
        stats.put("totalViews", totalViews);

        return stats;
    }
}
